
package me.zhengjie.modules.summary.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.summary.domain.RecordYear;
import me.zhengjie.modules.summary.service.RecordYearService;
import me.zhengjie.modules.summary.service.dto.RecordYearQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author yyj
* @date 2022-12-26
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "record管理")
@RequestMapping("/api/recordYear")
public class RecordYearController {

    private final RecordYearService recordYearService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('record:list')")
    public void download(HttpServletResponse response, RecordYearQueryCriteria criteria) throws IOException {
        recordYearService.download(recordYearService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询record")
    @ApiOperation("查询record")
    @PreAuthorize("@el.check('record:list')")
    public ResponseEntity<Object> query(RecordYearQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(recordYearService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增record")
    @ApiOperation("新增record")
    @PreAuthorize("@el.check('record:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody RecordYear resources){
        return new ResponseEntity<>(recordYearService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改record")
    @ApiOperation("修改record")
    @PreAuthorize("@el.check('record:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody RecordYear resources){
        recordYearService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除record")
    @ApiOperation("删除record")
    @PreAuthorize("@el.check('record:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody String[] ids) {
        recordYearService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}